{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 113,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import requests\n",
    "import time\n",
    "import math\n",
    "import json\n",
    "from bs4 import BeautifulSoup\n",
    "\n",
    "df = pd.read_excel('./test.xlsx')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 114,
   "metadata": {},
   "outputs": [],
   "source": [
    "# bs4提取文本\n",
    "def get_text_bs(html):\n",
    "    tree = BeautifulSoup(html, 'lxml')\n",
    "\n",
    "    body = tree.body\n",
    "    if body is None:\n",
    "        return None\n",
    "\n",
    "    for tag in body.select('script'):\n",
    "        tag.decompose()\n",
    "    for tag in body.select('style'):\n",
    "        tag.decompose()\n",
    "\n",
    "    text = body.get_text(separator='')\n",
    "    return text\n",
    "\n",
    "# 定义获取详细回答的通用函数\n",
    "def getAnswer(url, count):\n",
    "    # 必要参数\n",
    "    num = url[-9:]\n",
    "    freq = math.floor(count/5)\n",
    "    outArr = []\n",
    "    # 网络请求参数\n",
    "    baseUrl = 'https://www.zhihu.com/api/v4/questions/{}/answers?include=data%5B%2A%5D.is_normal%2Cadmin_closed_comment%2Creward_info%2Cis_collapsed%2Cannotation_action%2Cannotation_detail%2Ccollapse_reason%2Cis_sticky%2Ccollapsed_by%2Csuggest_edit%2Ccomment_count%2Ccan_comment%2Ccontent%2Ceditable_content%2Cattachment%2Cvoteup_count%2Creshipment_settings%2Ccomment_permission%2Ccreated_time%2Cupdated_time%2Creview_info%2Crelevant_info%2Cquestion%2Cexcerpt%2Cis_labeled%2Cpaid_info%2Cpaid_info_content%2Crelationship.is_authorized%2Cis_author%2Cvoting%2Cis_thanked%2Cis_nothelp%2Cis_recognized%3Bdata%5B%2A%5D.mark_infos%5B%2A%5D.url%3Bdata%5B%2A%5D.author.follower_count%2Cvip_info%2Cbadge%5B%2A%5D.topics%3Bdata%5B%2A%5D.settings.table_of_content.enabled&limit=5&offset={}&platform=desktop&sort_by=default'\n",
    "    headers = {\n",
    "        'Host':'www.zhihu.com',\n",
    "        'Connection':'keep-alive',\n",
    "        'x-zse-93':'101_3_2.0',\n",
    "        'x-ab-param':'se_ffzx_jushen1=0;pf_adjust=1;zr_slotpaidexp=1;tp_topic_style=0;tp_zrec=1;tp_contents=2;pf_noti_entry_num=2;zr_expslotpaid=1;top_test_4_liguangyi=1;qap_question_visitor= 0;tp_dingyue_video=0;qap_question_author=0',\n",
    "        'x-ab-pb':'Cr4B4wRWBYwCwQSeBaED9AMzBMoFRwD2AvQLGwDpBHUENwVQA4kM1wIyBaIDtQvKAhEF4AvHAg4FsQVpAcwCGQUBC+wKOwK3A08DjgNXBM8L2gRDBXQBCwRFBFEF1wsqA+EEmws/AA8LNAwpBYAFUgtXA6ADVQUyA6YE0QQ3DPgDNATgBMIFVgy0CmoBCgQ/BdwL5ApyAxIFUgXzA0IEtADWBLIFFQUzBQcMYAt9AmQEfwVDALkCFAVAAYQCnwLYAhJfAgAAAAAAAAAAAAAAAAEAAAAAAAAAAwAAAAAAAAABAAEBAAAAAAELAQAAAAEAAAEEAgABAAAAAQAAAAAABQEBAAEAAQAAAQAAAAEAAAAAAAAAAgEBAAEBARUVAgEAAgA=',\n",
    "        'sec-ch-ua-mobile':'?0',\n",
    "        'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.81 Safari/537.36',\n",
    "        'x-zst-81':'3_2.0aR_sn77yn6O92wOB8hPZnQr0EMYxc4f18wNBUgpTr7tuSRFqK6P0E69y-LS9-hp1DufI-we8gGHPgJO1xuPZ0GxCTJHR7820XM20cLRGDJXfgGCBxupMuD_Ie8FL7AtqM6O1VDQyQ6nxrRPCHukMoCXBEgOsiRP0XL2ZUBXmDDV9qhnyTXFMnXcTF_ntRueThXY9669GtqpBpG2Y4Ue1ywX1nqwVh9g8W9xBXcH1SBV_ZBYm1q3_wqOy2BYPvASLvrNfuDUmJ93_Yw3O1MxqADgmph2BkvOO0JSBVbLMQuosIq3_5Co1jwNMSTF_kCH9iCLL9wLfFvH8rRFB8DO8pup0Ecx8-9NmTvcVXDuGQJx1VuY05qfzeH3C3hc1QMSCYMOCrDC02qpm-bO88RCGoq3BawCmFqpMQBg9eTHCc9tLICV187SMQQV_twCYACLfsGL0-remJqLm1DpO8HLGHhV8jhp_r_V0uqXC2J9C',\n",
    "        'x-requested-with':'fetch',\n",
    "        'x-zse-96':'2.0_aLFBre9ygutxS0YB1LxB2irqFBxxFwxqGRtybHXBgBFx',\n",
    "        'sec-ch-ua':'\"Chromium\";v=\"94\", \"Google Chrome\";v=\"94\", \";Not A Brand\";v=\"99\"',\n",
    "        'sec-ch-ua-platform':'\"Windows\"',\n",
    "        'Accept':'*/*',\n",
    "        'Sec-Fetch-Site':'same-origin',\n",
    "        'Sec-Fetch-Mode':'cors',\n",
    "        'Sec-Fetch-Dest':'empty',\n",
    "        'Referer':'https://www.zhihu.com/question/20970974',\n",
    "        'Accept-Encoding':'gzip, deflate, br',\n",
    "        'Accept-Language':'zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7',\n",
    "        'Cookie':'_zap=7c6b1178-7ecf-4461-8102-605774e55301; d_c0=\"AICSY5B5gxGPTrOMPR5UTZEJEa6iT3yPENQ=|1593660904\"; _ga=GA1.2.1934862013.1593660905; _xsrf=ffe04MllUDtjKNWzNio25uYOXrASgbsq; __snaker__id=tAeJ0FlSoB76vsVm; _9755xjdesxxd_=32; YD00517437729195%3AWM_TID=nMOwYnz4Wx1BUQVRVEJ%2F1MBvS%2BaVFbM8; __utmv=51854390.100--|2=registration_date=20210317=1^3=entry_date=20200709=1; __utmz=51854390.1626961624.2.2.utmcsr=zhihu.com|utmccn=(referral)|utmcmd=referral|utmcct=/question/473802687; gdxidpyhxdE=yI8wbeZ%5CHpQGXIQ%2BguZ%5CCRgqa5dRop%5CLS3HsQNmUpeZk9hd8YxAHTS3bzmKSXMwx3PB9tXZJ%2FViYLs1mvumf%2B1yi9pmNqNvjwadnXItJucSaxl9aldBDRcC3AoeN4wB%2FbN8neBDibevqdIi1RZYlEoKZb2bG6KqHhXPTUneGu50bGsqL%3A1634660927156; YD00517437729195%3AWM_NI=c90cP4Q2oGizDeBphXp%2BeXehv0y%2B2S9SNiiZqrmBvmncbVdnkNYQr9D%2BaNiNZFLOp0lCbA2WpIVdwiSMjRxRrxlJOvIrK5ZKhuHHcZRQvVBgK2gxbGVPdaQEuV5FaLgaNE4%3D; YD00517437729195%3AWM_NIKE=9ca17ae2e6ffcda170e2e6eea2c460a6ae8fa4e65a8ae78ab3c14e838f8e84f4799bba9daeec5083f18f85d12af0fea7c3b92aa1e70084d34093eea1a8ec7288bb8e91eb54b4f5abb9e864b6f0b7ccb159fb8d828aec52b3aea3a3f567aebdaeb7e766a99297b2f13388f5afafec469a9a84b0f33cb09daea2b15db4979a85f6639193a6b3fb5ab2a8a4abce7e8bae85d6d941acab88a9e252928fbab7c16d89eea999e14d9690ac82d44fb78afba7e448a5bf82a6b737e2a3; l_cap_id=\"N2M5ZjIxOGI0MGVlNGRlMzgyMDUzM2RiMzcwYjkxN2U=|1634660049|987bf4c70953a7ef070519d816b97bf2d232a512\"; r_cap_id=\"ODdlMTgwMDI2ZjQ5NGUyOWE2MjgxYzAyZWJjZTc1NjQ=|1634660049|017dca5083291d30771993d1b47a7eb8fc0e12ae\"; cap_id=\"NGU4MmZkNTFlY2NlNDU0ZTgzNjQyNzUyZjFmMzg4Y2Q=|1634660049|521fb82a5206009b77722e9429ccc333a09e38f5\"; captcha_session_v2=\"2|1:0|10:1634660114|18:captcha_session_v2|88:SUp2bTZZTjM3dHFDRERJQmFBWC9aemJ6WXhkSjBCL05VUG5WQlpld09wdk0zTzRSdG1CczlhWTloVDRTQUV1Mw==|3533890e2d2c98b2603d05d00ca5ac6ee12b7bd0ea0cfc5795beb03b837d1270\"; captcha_ticket_v2=\"2|1:0|10:1634660122|17:captcha_ticket_v2|704:eyJ2YWxpZGF0ZSI6IkNOMzFfMHZSLjV4cFhKZ2RhMkNJY0JoMDhma2pLZEs3YXljTDgyb1BtSy11SF9aSmFhN3lpT1R6WGlMbUVqQ1RWQUNjX2JHdzdMbmg4eDB0YWRKRWk2LWJTWnlmZ1pXVXRsOGJmR20uZ3ROVkh3U0lYWnZBMW80cGZDdE5LUmdSdnF5RjJzZmljb1BkbVJ4aVN6WHRuc256MEVCbUUwUVdqZ3d5cGVXeFkuLU1aLlJCQzFIRGtzaTBVcmp1aVk0cS5LbWNDaEQ1QTB5R2ZialhlZmxwRXpMaFBXd25tZUU2NGh2Q24wMDJsMmFfd0ZfTElJb3ZkN0FJTjBidkJaUVJyZlBnb0VOVlRrLU92LVRxNFRCc3RqeVZNYUFqcGEudnNTcndlcmpjSFk0al9MS19NRDFtMVJDXzFXWFphNERsc1lZVVNiRnlqdW1KdFFIcVlsUkxIXy1ra1Y3cTJVRGhKSEZxMGx1a1lNcHZldVcuMHRJc2xqNXNwRC1PNXVvVnd5cERieWVXaHpnUGJ0NHNaNkNKaXJJZC0tcGcwZHc5RVdKcDVUeWs5Zng5QXlINjUxR21DNWtuWi1rRDhCUlUtMEV6c2JSTW9JY1N3SjcySEZoLkVmbjI3RnRnQnAuVXFWaGJ5eHJjY2JyUHhYLm9NS3FULkZWX3l6RC1GeTBaMyJ9|6f750776e2778fb05170e69ba09def54c9f66701cd6c7aa7e105b164e67a8c0d\"; z_c0=\"2|1:0|10:1634660139|4:z_c0|92:Mi4xZWlTakt3QUFBQUFBZ0pKamtIbURFU1lBQUFCZ0FsVk5LejFjWWdBNkJ3Q0piUE4xTks4Q2Z4MHg2bEoyY0s4a0NB|f65065f1a250e2cfc49e57d444ac431c50107835a3ca3cc46307691af77e2810\"; q_c1=ad266df5c57c402098b8dbc61971e450|1634660266000|1594278698000; __utma=51854390.1934862013.1593660905.1634660267.1634699792.8; tst=r; Hm_lvt_98beee57fd2ef70ccdd5ca52b9740c49=1634563375,1634660025,1634699788,1634716706; SESSIONID=u11jjwjqFSWRANBpTcqcKkm5Mip3O82Aq4uM5RPnBb7; JOID=VV0SAU_eWfGpKb7uPtt4qDflZX4vqWrFxUDLhkqbCKT-bvOuTPiSnc0ovuk9-f3A6mvV6nc2K3_CqOMP5-9TLUY=; osd=W10UCknQWfeiL7DuONB-pjfjbnghqWzOw07LgEGdBqT4ZfWgTP6Zm8MouOI79_3G4W3b6nE9LXHCrugJ6e9VJkA=; Hm_lpvt_98beee57fd2ef70ccdd5ca52b9740c49=1634720111; KLBRSID=37f2e85292ebb2c2ef70f1d8e39c2b34|1634720152|1634716215',\n",
    "        'Content-Type':'text/plain'\n",
    "    }\n",
    "    # 按频次遍历并提取信息\n",
    "    for i in range(1, freq+1):\n",
    "#         try:\n",
    "            reqUrl = baseUrl.format(num, i*5)\n",
    "            res = requests.get(reqUrl, headers=headers)\n",
    "            loadedData = json.loads(res.text)['data']\n",
    "            for i in loadedData:\n",
    "                author = i['author']['name']\n",
    "                authorUrl = i['author']['url']\n",
    "                isOrg = i['author']['is_org']\n",
    "                followerCount = i['author']['follower_count']\n",
    "                gender = i['author']['gender']\n",
    "                commentNum = i['comment_count']\n",
    "                voteupNum = i['voteup_count']\n",
    "                answerUrl = i['url']\n",
    "                # 解析时间\n",
    "                timeStamp = i['updated_time']\n",
    "                timeArray = time.localtime(int(timeStamp)) \n",
    "                updatedTime = time.strftime(\"%Y-%m-%d %H:%M:%S\", timeArray)\n",
    "                # 文本提取(调用了一个第三方库)\n",
    "                text = i['content']\n",
    "                content = get_text_bs(text)\n",
    "                # 问题信息\n",
    "                questionTitle = i['question']['title']\n",
    "                questionUrl = i['question']['url']\n",
    "                questionID = i['question']['id']\n",
    "                quetionCreatedTimestamp = i['question']['created']\n",
    "                questionTimeArray = time.localtime(int(quetionCreatedTimestamp)) \n",
    "                quetionCreatedTime = time.strftime(\"%Y-%m-%d %H:%M:%S\", questionTimeArray)\n",
    "\n",
    "                outArr.append([author, authorUrl, isOrg, followerCount, gender, commentNum, voteupNum, answerUrl, updatedTime, content, questionTitle, questionUrl, questionID, quetionCreatedTime])\n",
    "#         except:\n",
    "#             outArr.append([None, None, None, None, None, None, None, None, None, None, None, None, None, None])\n",
    "        \n",
    "    return outArr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 115,
   "metadata": {},
   "outputs": [
    {
     "ename": "JSONDecodeError",
     "evalue": "Expecting value: line 1 column 1 (char 0)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mJSONDecodeError\u001b[0m                           Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-115-95a6517fd1f1>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      2\u001b[0m \u001b[0mcount\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;36m0\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      3\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0mi\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mdf\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'问题主页'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 4\u001b[1;33m     \u001b[0mresArr\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mgetAnswer\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mi\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m10\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      5\u001b[0m     \u001b[1;32mfor\u001b[0m \u001b[0mv\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mresArr\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      6\u001b[0m         \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mcount\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32m<ipython-input-114-32e6afa05b2a>\u001b[0m in \u001b[0;36mgetAnswer\u001b[1;34m(url, count)\u001b[0m\n\u001b[0;32m     51\u001b[0m             \u001b[0mreqUrl\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mbaseUrl\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mformat\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mnum\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mi\u001b[0m\u001b[1;33m*\u001b[0m\u001b[1;36m5\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     52\u001b[0m             \u001b[0mres\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mrequests\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mget\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mreqUrl\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mheaders\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mheaders\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 53\u001b[1;33m             \u001b[0mloadedData\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mjson\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mloads\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mres\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mtext\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'data'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     54\u001b[0m             \u001b[1;32mfor\u001b[0m \u001b[0mi\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mloadedData\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     55\u001b[0m                 \u001b[0mauthor\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mi\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'author'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'name'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mc:\\users\\86173\\appdata\\local\\programs\\python\\python37\\lib\\json\\__init__.py\u001b[0m in \u001b[0;36mloads\u001b[1;34m(s, encoding, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)\u001b[0m\n\u001b[0;32m    346\u001b[0m             \u001b[0mparse_int\u001b[0m \u001b[1;32mis\u001b[0m \u001b[1;32mNone\u001b[0m \u001b[1;32mand\u001b[0m \u001b[0mparse_float\u001b[0m \u001b[1;32mis\u001b[0m \u001b[1;32mNone\u001b[0m \u001b[1;32mand\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    347\u001b[0m             parse_constant is None and object_pairs_hook is None and not kw):\n\u001b[1;32m--> 348\u001b[1;33m         \u001b[1;32mreturn\u001b[0m \u001b[0m_default_decoder\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mdecode\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0ms\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    349\u001b[0m     \u001b[1;32mif\u001b[0m \u001b[0mcls\u001b[0m \u001b[1;32mis\u001b[0m \u001b[1;32mNone\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    350\u001b[0m         \u001b[0mcls\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mJSONDecoder\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mc:\\users\\86173\\appdata\\local\\programs\\python\\python37\\lib\\json\\decoder.py\u001b[0m in \u001b[0;36mdecode\u001b[1;34m(self, s, _w)\u001b[0m\n\u001b[0;32m    335\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    336\u001b[0m         \"\"\"\n\u001b[1;32m--> 337\u001b[1;33m         \u001b[0mobj\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mend\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mraw_decode\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0ms\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0midx\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0m_w\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0ms\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m0\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mend\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    338\u001b[0m         \u001b[0mend\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0m_w\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0ms\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mend\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mend\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    339\u001b[0m         \u001b[1;32mif\u001b[0m \u001b[0mend\u001b[0m \u001b[1;33m!=\u001b[0m \u001b[0mlen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0ms\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mc:\\users\\86173\\appdata\\local\\programs\\python\\python37\\lib\\json\\decoder.py\u001b[0m in \u001b[0;36mraw_decode\u001b[1;34m(self, s, idx)\u001b[0m\n\u001b[0;32m    353\u001b[0m             \u001b[0mobj\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mend\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mscan_once\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0ms\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0midx\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    354\u001b[0m         \u001b[1;32mexcept\u001b[0m \u001b[0mStopIteration\u001b[0m \u001b[1;32mas\u001b[0m \u001b[0merr\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 355\u001b[1;33m             \u001b[1;32mraise\u001b[0m \u001b[0mJSONDecodeError\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"Expecting value\"\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0ms\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0merr\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mvalue\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;32mfrom\u001b[0m \u001b[1;32mNone\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    356\u001b[0m         \u001b[1;32mreturn\u001b[0m \u001b[0mobj\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mend\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mJSONDecodeError\u001b[0m: Expecting value: line 1 column 1 (char 0)"
     ]
    }
   ],
   "source": [
    "out = pd.DataFrame(columns=('作者', '作者主页', '是否为机构号', '关注者总数', '性别', '评论数', '赞数', '回答链接', '回答时间', '回答内容', '问题标题', '问题链接', '问题ID', '问题提问时间'))\n",
    "count = 0\n",
    "for i in df['问题主页']:\n",
    "    resArr = getAnswer(i, 10)\n",
    "    for v in resArr:\n",
    "        print(count)\n",
    "        out.loc[count] = v\n",
    "        count += 1\n",
    "print('Scrape Over')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "out.to_excel('./comments.xlsx')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
